﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 任务服务
    /// </summary>
    public class TaskService : ITaskService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Task model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(List<string> ids)
        {
            return dal.Updateable<T_Task>(new
            {
                DataStatus = DataStatusEnum.Deleted,
                ModifyTime = DateTime.Now,
            }).Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;
        }

        public T_Task GetById(string id)
        {
            return dal.Get<T_Task>(id);
        }

        public List<T_Task> GetList(string publisherCompanyID, string subscriberCompanyID)
        {
            Expression<Func<T_Task, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(publisherCompanyID))
            {
                whereExp = whereExp.Compose((a) => a.PublisherCompanyID == publisherCompanyID, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(subscriberCompanyID))
            {
                whereExp = whereExp.Compose((a) => a.SubscriberCompanyID == subscriberCompanyID, Expression.AndAlso);
            }
            return dal.Queryable<T_Task>().Where(whereExp).ToList();
        }

        public PageData<T_Task> Query(string publisherCompanyID, string subscriberCompanyID, string taskNo, TaskSourceTypeEnum taskSourceType, TaskCategoryEnum taskCategory, TaskStatusEnum taskStatus, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Task, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(publisherCompanyID))
            {
                whereExp = whereExp.Compose((a) => a.PublisherCompanyID == publisherCompanyID, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(subscriberCompanyID))
            {
                whereExp = whereExp.Compose((a) => a.SubscriberCompanyID == subscriberCompanyID, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(taskNo))
            {
                whereExp = whereExp.Compose((a) => a.TaskNo.StartsWith(taskNo), Expression.AndAlso);
            }

            List<T_Task> list = dal.Queryable<T_Task>().Where(whereExp).OrderBy(a => a.CreateTime, SqlSugar.OrderByType.Desc).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Task> { Data = list, Page = page };
        }

        public bool Update(T_Task model)
        {
            return dal.Modify(model);
        }
    }
}
